<?php

// $Id$

require_once(TANGRA_MAIN_DIR.'form/gform_ctrl_with_obj.class.php');

require_once('vpt_ci_form.class.php');
require_once('vpt_ci_form_view.class.php');

require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/vpage/tcms_vpt_ci_dbc.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/ci/tcms_ci_type_dbc.class.php');


class VPT_CI_Form_Ctrl extends GForm_Ctrl_With_Object {
	private $hidden_path;
	private $types_ol_map;

	function __construct($system_name, Vars_Manager $vm, DB_Connection $dbc, $object_id_capture = 'obj_id', $hidden_path) {
		$this->hidden_path = tangra_normalize_path($hidden_path);
		parent::__construct($system_name, $vm, $dbc, $object_id_capture);

		$this->export('hidden_path', $this->hidden_path.'inc/');
	}


	protected function create_form() {
		$form = new VPT_CI_Form();

		$dbc = $this->get_dbc();

		$ol_map = TCMS_CI_Type_DBC::load_types_ol_map($dbc);
		$types_field = $form->get_field('ci_type');
		array_unshift($ol_map['ids'], 0);
		$ol_map['ol_map'][0] = '---';

		$types_field->set_options($ol_map['ids']);
		$this->types_ol_map = $ol_map['ol_map'];


//		$form->generate_html();
		return $form;
	}


	protected function create_form_view() {
		$ret = new VPT_CI_Form_View($this->form);

		$types_field = $ret->get_field_view('ci_type');
		$types_field->set_ol_map($this->types_ol_map);


		return $ret;
	}


	protected function on_good_submit() {
		$ret = true;

		if (!ereg("^[a-z]{1}[a-z0-9_]{0,254}$", $this->form->get_field_value('export_name'))) {
			$this->form->set_field_error('export_name', 'invalid_export_name');
			$ret = false;
		} else {
			$dbc = $this->get_dbc();
			$object = $this->get_object();
			if (TCMS_VPT_CI_DBC::is_unique_template_plus_export_name($dbc,
																				$object->get_id(),
																				$this->get_permanent_external_param('template'),
																				$this->form->get_field_value('export_name'))) {
				$this->form->set_field_error('export_name', 'duplicate_export_name');
				$ret = false;
			}
		}

		return $ret;
	}


	protected function on_bad_submit() {

//		printbr('lajna');
	}


	protected function create_object() {
		$object = new TCMS_VPT_CI_DBC();

		return $object;
	}


	protected function transfer_obj2form() {
		$object = $this->get_object();
		$form = $this->get_form();

		$form->set_field_value('ci_name', $object->get_ci_name());
		$form->set_field_value('ci_type', $object->get_ci_type());
		$form->set_field_value('required', $object->get_required());
		$form->set_field_value('export_name', $object->get_export_name());
	}


	protected function transfer_form2obj() {
		$object = $this->get_object();
		$form = $this->get_form();

		$object->set_ci_name($form->get_field_value('ci_name'));
		$object->set_ci_type($form->get_field_value('ci_type'));
		$object->set_required($form->get_field_value('required'));
		$object->set_export_name($form->get_field_value('export_name'));

		if (!$object->get_id()) {
			$object->set_template($this->get_permanent_external_param('template'));
		}
	}
}